home *** CD-ROM | disk | FTP | other *** search
Wrap
Text File | 1989-04-24 | 10.2 KB | 410 lines | [ TEXT/PJMM]
{• •} {• Auteur: Eric Carrasco •} {• © 1989 •} {• Société CFJ Consultant •} {• Conseil, Développement, Formations •} {• Paris, le Lundi 24 Avril 1989 •} {• •} {• •} {• Démonstration d'application adaptée à MultiFinder. •} {• Utilisation du Notification Manager lors du passage en arrière plan •} PROGRAM MFAndNotificationDemo; { Variables de compilation conditionnelle } {$DECL MPW } {$SETC MPW := 0 } { 0 : THINK Pascal ; 1 : MPW } {$DECL Debug } {$SETC Debug := 1 } { 0 : Final ; 1 : Debugging } {$IFC MPW} USES MemTypes, Quickdraw, OsIntf, ToolIntf, {standard lib} PackIntf, SANE, Notification; PROCEDURE _DataInit; EXTERNAL; {$IFC Debug} {$D+} { Trace des procédures } {$L+} { Listing du programme autorisé } {$ELSEC} {$D-} { Suppression des noms de procédures } {$L-} { Pas de listing des Units } {$ENDC} { END IF Debug } {$ELSEC} USES Notification; {$I-} { Pas d'initialisation automatique avec THINK Pascal } {$ENDC} { END IF MPW } {$IFC Debug} PROCEDURE MacsBugPrint (VAR BugStr: Str255); INLINE $ABFF; PROCEDURE Dump (S: Str255; Err: LONGINT; Int: LONGINT); VAR tmpSa: Str255; tmpSb: Str255; BEGIN NumToString(Err, tmpSa); NumToString(Int, tmpSb); S := CONCAT('From : ', S, ' -- Param1= ', tmpSa, ' -- Param2= ', tmpSb); MacsBugPrint(S); END; { Dump } {$ENDC} { END IF Debug } CONST { SysEnvirons } versionRequested = 1; { MultiFinder } kSuspendOrResume = $01; kMouseMovedMessage = $FA; { WaitNextEvent } foreGroundSleep = 1; {temps maximum donné aux autres applis (en tics) } backGroundSleep = 50; {temps maximum donné aux autres applis (en tics) } { Count } theDialogID = 128; line1 = 15; line2 = 30; line3 = 45; line4 = 60; line5 = 75; { Gestion des alertes } theALRTid = 129; VAR { MultiFinder } MultiFinderActive, WNEIsImplemented: Boolean; { SysEnvirons } theWorld: SysEnvRec; { Gestion des erreurs } err: OSErr; { WaitNextEvent } Done: Boolean; sleep: LongInt; RegainControl, convertClipboard: Boolean; { Count } i: LongInt; theDialog: DialogPtr; lastState: Boolean; aRect, bRect, cRect, dRect, eRect: Rect; aString: Str255; { Notification } aNMRec: NMRec; { System 6.0 and higher } notifyLevel: integer; nmString: str255; {------------------------------------------ nmResponse ---------------------------------------} { Response procedure for Notification Manager (System ≥ 6.0). } PROCEDURE nmResponse (aNmReqPtr: QElemPtr); BEGIN { We won't do anything } END; { PROC nmResponse } {------------------------------------------ notify ---------------------------------------} { Response procedure for Notification Manager (System ≥ 6.0). } PROCEDURE notify; BEGIN IF theWorld.systemVersion < $0600 THEN BEGIN IF notifyLevel > 0 THEN SysBeep(5); EXIT(notify); END; { IF theWorld.systemVersion < $0600 } WITH aNMRec DO BEGIN qType := 8; { 8 = ORD(nmType) } IF notifyLevel > 1 THEN BEGIN nmSIcon := GetResource('SICN', 128); nmString := CONCAT('Message form Eric, using the Notification Manager:', CHR($0D), 'Note that this message comes from a backGround application.', CHR($0D), ' Note also the Apple menu : an Eiffel tour is blinking and a "" marks the application name.'); nmStr := @nmString; END; IF ODD(notifyLevel) THEN nmSound := Handle(ORD4(-1)); END; { WITH aNMRec } Err := NMInstall(@aNMRec); END; { PROC notify } {------------------------------------------ CheckNotification ---------------------------------------} PROCEDURE CheckNotification; BEGIN WITH aNMRec DO BEGIN qType := 0; { queue type -- ORD(nmType) = 8 when in use } nmMark := 1; { item to mark in Apple menu } nmSIcon := NIL; { handle to small icon } nmSound := NIL; { handle to sound record } nmStr := NIL; { string to appear in alert } nmResp := @nmResponse;{ pointer to response routine } nmRefCon := 0; { for application use } END; { WITH aNMRec } IF NOT MultiFinderActive THEN notifyLevel := 0 ELSE IF theWorld.systemVersion < $0600 THEN notifyLevel := 1 ELSE notifyLevel := 3; END; { PROC CheckNotification } {----------------------------------------- doUpdate --------------------------------------} PROCEDURE doUpDate; BEGIN BeginUpdate(theDialog); EraseRect(aRect); MoveTo(5, line1); IF WNEIsImplemented THEN DrawString('WaitNextEvent is implemented') ELSE DrawString('WaitNextEvent is not implemented'); EraseRect(bRect); MoveTo(5, line2); IF MultiFinderActive THEN DrawString('MultiFinder is active') ELSE DrawString('MultiFinder is not active'); EraseRect(dRect); NumToString(Sleep, aString); MoveTo(5, line4); IF RegainControl THEN BEGIN DrawString(CONCAT('foreGround, sleep time =', aString)); END ELSE BEGIN DrawString(CONCAT('backGround, sleep time =', aString)); END; { IF RegainControl } EndUpdate(theDialog); EraseRect(eRect); END; { PROC doUpdate } {------------------------------------------- Count ----------------------------------------} { Procedure très mal écrite } PROCEDURE Count (i: LongInt); BEGIN IF theDialog = NIL THEN Exit(Count); EraseRect(cRect); NumToString(i, aString); MoveTo(5, line3); DrawString(aString); IF lastState <> RegainControl THEN BEGIN EraseRect(dRect); lastState := RegainControl; NumToString(Sleep, aString); MoveTo(5, line4); IF RegainControl THEN BEGIN DrawString(CONCAT('foreGround, sleep time =', aString)); END ELSE BEGIN DrawString(CONCAT('backGround, sleep time =', aString)); END; { IF RegainControl } END; { IF lastState <> RegainControl } END; { PROC Count } {------------------------------------------- MainEventLoop ----------------------------------------} PROCEDURE MainEventLoop; VAR { WaitNextEvent } EventFlag: boolean; Event: EventRecord; BEGIN i := 0; REPEAT i := SUCC(i); Count(i); IF WNEIsImplemented THEN EventFlag := WaitNextEvent(EveryEvent, Event, sleep, NIL) ELSE BEGIN SystemTask; EventFlag := GetNextEvent(everyEvent, Event); END; IF EventFlag THEN {HandleEvent} CASE Event.what OF keyDown: BEGIN Done := TRUE; END; { keyDown } updateEvt: BEGIN doUpdate; END; { updateEvt } app4Evt: BEGIN (* Récupérons l'octet de poid fort (bits 24-31) *) CASE BSR(BAND(Event.message, $FF000000), 24) OF kSuspendOrResume: BEGIN (* Si "message" est pair, nous recevons un resumeEvt, *) (* sinon un suspendEvt *) RegainControl := Odd(Event.message); (* devons-nous coercer le presse-papiers? *) convertClipboard := BAND(Event.message, $00000002) <> 0; EraseRect(eRect); MoveTo(5, line5); IF RegainControl THEN BEGIN (* Nous reprenons le contrôle *) DrawString('Resume'); sleep := foreGroundSleep; Err := NMRemove(@aNMRec); END ELSE BEGIN (* Nous perdons le contrôle *) DrawString('Suspend'); sleep := backGroundSleep; notify; END; { IF RegainControl } END; (* kSuspendOrResume *) kMouseMovedMessage: BEGIN {• AdjusCursor;•} END; (* kMouseMovedMessage *) END; (* CASE BSR(BAND(message, $FF000000), 24) *) END; { app4Evt } END; { CASE Event.what } UNTIL Done OR (i > 2147483647 - 1); END; { PROC MainEventLoop } {------------------------------------------- CheckMultiFinder ----------------------------------------} PROCEDURE CheckMultiFinder; CONST WNETrapNum = $01; MFTrapNum = $8F; UnImplTrapNum = $9F; (* Cette trap ne sera jamais implementée *) BEGIN (* Vérifier si la version du system est supérieure à 4.1 *) (* Sinon, MultiFinder ne pourrait fonctionner *) WITH theWorld DO BEGIN IF (Err <> noErr) OR (systemVersion < $0410) THEN BEGIN MultiFinderActive := FALSE; Err := StopAlert(theALRTid, NIL); ExitToShell; (* Nous avons tant besoin du system 4.1! *) END ELSE BEGIN (* Savoir si WaitNextEvent est implémenté peut toujours etre utile *) WNEIsImplemented := (machineType >= 0) AND (NGetTrapAddress(WNETrapNum, ToolTrap) <> NGetTrapAddress(UnImplTrapNum, ToolTrap)); (* Technique à revoir à partir du system 7.0 *) MultiFinderActive := (machineType >= 0) AND (NGetTrapAddress(MFTrapNum, ToolTrap) <> NGetTrapAddress(UnImplTrapNum, ToolTrap)); END; (* IF (Err <> noErr) OR (systemVersion < $0410) *) END; (* WITH theWorld DO *) END; { PROC CheckMultiFinder } BEGIN { PROGRAM MFAndNotificationDemo } { Initialization (impératives pour Mac II avec LSP) } {$IFC MPW} UnLoadSeg(@_DataInit); {remove MPW data initialization code before any allocations} {$ENDC} { END IF MPW } InitGraf(@thePort); {initialize QuickDraw} InitFonts; {initialize Font Manager} FlushEvents(everyEvent, 0); {call OS Event Mgr to discard any previous events} InitWindows; {initialize Window Manager} InitMenus; {initialize Menu Manager} TEInit; {initialize TextEdit} InitDialogs(NIL); {initialize Dialog Manager} InitCursor; {call QuickDraw to make cursor (pointer) an arrow} err := SysEnvirons(versionRequested, theWorld); Done := FALSE; sleep := foreGroundSleep; RegainControl := TRUE; lastState := NOT RegainControl; theDialog := NIL; theDialog := GetNewDialog(theDialogID, NIL, Pointer(-1)); IF theDialog = NIL THEN BEGIN SysBeep(5); SysBeep(5); SysBeep(5); ExitToShell; END; { IF theDialog = NIL } SetPort(theDialog); ShowWindow(theDialog); CheckMultiFinder; CheckNotification; aRect := theDialog^.PortRect; EraseRect(aRect); bRect := aRect; cRect := aRect; dRect := aRect; eRect := aRect; aRect.bottom := line1 + 3; bRect.top := line1 + 3; bRect.bottom := line2 + 3; cRect.top := line2 + 3; cRect.bottom := line3 + 3; dRect.top := line3 + 3; dRect.bottom := line4 + 3; eRect.top := line4 + 3; eRect.bottom := line5 + 3; MainEventLoop; END. { PROGRAM MFAndNotificationDemo }